<%@ page import="org.apache.log4j.Logger" %>
<%@ page import="it.trekker.prefetch.IExcursionDAOWithPrefetch" %>
<%@ page import="it.trekker.upload.json.IFileLoaderFactory" %>
<%@ page import="it.elbuild.jcoord.LatLng" %>
<%@ page import="it.elbuild.jcoord.resolver.GeoCodeResolver" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="it.trekker.dao.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="it.trekker.upload.*" %>
 
<%!
String printExcursionTableRow(Excursion excursion) {
	
	if(excursion == null)
		return "";
	
	SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy - HH:mm:ss");
	StringBuffer html = new StringBuffer();
	
	html
		.append("<tr>")
		.append("<td>")
		.append("<a href='../excursion_details.jsp?id=" + excursion.getId() + "'>")
			.append(excursion.getId())
		.append("</a>")
		.append("</td>")
		.append("<td>")
		.append(excursion.getName())
		.append("</td>")
		.append("<td>")
		.append(df.format(excursion.getDate()))
		.append("</td>")
		.append("<td>")
		.append(excursion.getUser().getUsername())
		.append("</td>")
		.append("<td>")
		.append("<a href='index.jsp?op=delete&id=" + excursion.getId() + "'>delete</a>")
		.append("</td>")
		.append("</tr>");
	
	return html.toString(); 
}

String printExcursionTableRows(List<Excursion> excursions) {
	if(excursions == null)
		return "";
	
	StringBuffer html = new StringBuffer();
	
	for(Excursion excursion : excursions) {
		html.append(printExcursionTableRow(excursion));
	}	
	
	return html.toString(); 
}
%>

<%
Logger logger = Logger.getLogger("it.trekker.users.index.jsp");

DAOFactory daoFactory = DAOFactory.getDAOFactory(application.getInitParameter("dao"));
IExcursionDAO excursionDAO = daoFactory.getExcursionDAO();
IUserDAO userDAO = daoFactory.getUserDAO();

List<Excursion> excursionList = new ArrayList<Excursion>();
String principalName = request.getUserPrincipal().getName();

String op = request.getParameter("op");
if(op != null && op.equals("delete")) {
	int excursionId = Integer.parseInt(request.getParameter("id"));
	
	// Check if delete request comes from the owner of the excursion with id excursionId
	Excursion excursionToDelete = excursionDAO.findExcursionById(excursionId);
	if(excursionToDelete.getUser().getUsername().equals(principalName))
		excursionDAO.remove(excursionId);
}

if(principalName != null && !principalName.isEmpty()) {
	User user = userDAO.findUserByName(principalName);
	
	if(user != null) {
		excursionList = excursionDAO.findExcursionsByUser(user);
	}
}
%>

<html>

	<head>
		<title>Trekker</title>
	
		<meta http-equiv="Pragma" content="no-cache"/>
		<meta http-equiv="Expires" content="Mon, 01 Jan 1996 23:59:59 GMT"/>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<meta name="Author" content="you">

		<link rel="StyleSheet" href="../styles/default.css" type="text/css" media="all" />
	</head>
	
	<body>
		<div id="menu_link">
			<a href="../index.jsp">Back to home</a><br />
		</div>
		
		<h1>Your excursions</h1>
		
		<div>
			<table class="excursions">
				<thead>
					<tr>
						<th>Id</th>
						<th>Name</th>
						<th>Date</th>
						<th>User</th>
						<th>Actions</th>
					</tr>
				</thead>
				<%=	printExcursionTableRows(excursionList) %> 
			</table>
		</div>
	</body>

</html>
